<template>
  <div class="header-content">
    <div class="header-left flex-box">
      <el-icon size="20" class="icon" @click="store.commit('CollapseMenu')"
        ><Menu
      /></el-icon>
      <ul class="flex-box">
        <li
          v-for="(item, index) in selectMenu"
          :key="item.path"
          :class="{ selected: item.path == route.path }"
          class="tab flex-box"
        >
          <el-icon size="12"> <component :is="item.icon"></component></el-icon>
          <router-link class="flex-box text" :to="item.path">
            {{ item.name }}</router-link
          >

          <el-icon size="12" class="close" @click="closeTab(item, index)">
            <Close
          /></el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
      <el-dropdown>
        <div class="el-dropdown-link">
          <div>
            <el-avatar
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            />
          </div>
          <div class="user-name">admin</div>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>Action 1</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { useStore } from 'vuex';
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';

// 拿到store实例
const store = useStore();

// 拿到当前页面路由的实例
const route = useRoute();

// 拿到当前路由
const router = useRouter();

const selectMenu = computed(() => {
  return store.state.menu.selectMenu;
});
// console.log('selectMenu', selectMenu);

// 点击关闭tab
const closeTab = (item, index) => {
  // console.log(item, index);
  store.commit('closeMenu', item);
  // 删除的非当前页tag
  if (route.path !== item.path) {
    return;
  }
  // 计算属性要拿到值要用。value
  // 计算属性的返回值自动被包装成ref对象。如果你需要访问原始值，你可以通过.value属性来获取
  const selectMenuData = selectMenu.value;
  // console.log('selectMenuData', selectMenuData);
  //删除最后一项
  if (index == selectMenuData.length) {
    // 如果tag只有一个元素
    if (!selectMenuData.length) {
      router.push('/');
    } else {
      router.push({
        path: selectMenuData[index - 1].path,
      });
    }
  } else {
    // 如果删除的是中间位置的tag
    router.push({
      path: selectMenuData[index].path,
    });
  }
};
</script>

<style lang="less" scoped>
.flex-box {
  display: flex;
  align-items: center;
  height: 100%;
}
.header-content {
  background-color: rgb(255, 255, 255);
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
  .header-left {
    height: 100%;
    .icon {
      width: 45px;
      height: 100%;
    }
    .icon:hover {
      background-color: #f5f5f533;
      cursor: pointer;
    }
    .tab {
      padding: 0 10px;
      height: 100%;
      .text {
        margin: 0 5px;
      }
      .close {
        visibility: hidden;
      }
    }
    .tab:hover {
      background-color: #f5f5f5;
      .close {
        visibility: inherit;
        color: #333;
        cursor: pointer;
      }
      &.selected {
        a {
          color: #409eff;
        }
        i {
          color: #409eff;
        }
        background-color: #f5f5f5;
      }
    }
  }
  .header-right {
    .el-dropdown-link {
      display: flex;
      .user-name {
        margin: auto 0;
      }
    }
  }
  a {
    height: 100%;
    color: #333;
    font-size: 15px;
  }
}
</style>
